<template>
    <div>
        <div class="leftList comm-list" v-infinite-scroll="load" :style="{ height: stateHooks.leftHeight + 'px' }">
            <div class="item-box" @click="clickItem(index)" :class="listI === index ? 'acitve' : ''"
                v-for="(item, index) in listData" :key="index" infinite-scroll-distance="100">
                <div>
                    <div v-for="(row, i) in templates" :key="i">
                        <p :class="row.class" v-if="row.type == 'init'">{{ item[row.key] }}</p>
                        <slot :row="item" v-if="row.type == 'slot'" :name="row.key"></slot>
                    </div>
                </div>
            </div>
            <p class="flex_center font_black9 mb20" v-if="lLoading">
                <i class="el-icon-loading" style="font-size: 16px;"></i>
                <span class="ml5"> 加载中...</span>
            </p>
            <p class="flex_center font_black9 mb20" v-if="!lLoading && lNoMore">没有更多了</p>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, defineProps, defineEmits, watch } from '@vue/composition-api'
import { useCommonHooks } from '@/hooks/useCommonHooks.js'
// 使用全局状态
const { stateHooks } = useCommonHooks()

const props = defineProps({
    templates: {
        type: Array,
        required: true
    },
    listData: {
        type: Array,
        required: true
    },
    selectedIndex: {
        type: Number,
        default: 0,
        required: false
    },
    lLoading: {
        type: Boolean,
        default: false
    },
    lNoMore: {
        type: Boolean,
        default: false
    },
})

const emit = defineEmits(['update:selectedIndex', 'loadFun'])
const listI = ref(0)
watch(() => props.selectedIndex, (newValue) => {
    if (newValue != null) {
        console.log("listI=", newValue)
        listI.value = newValue
    } else {
        listI.value = null
    }
})

const clickItem = (index) => {
    if (listI.value == index) {
        return
    }
    emit('update:selectedIndex', index)
    emit('clickItem', index)
}

const load = () => {
    console.log("props.lNoMore=xx--", props.lNoMore)
    if (props.lNoMore) return
    setTimeout(() => {
        emit('loadFun')
    }, 300);
}

</script>

<style lang="scss" scoped>
div {
    box-sizing: border-box;
}

.leftList {
    box-sizing: border-box;
    padding-right: 2px;
    font-size: 14px;
    margin-top: 10px;
    // background: red;
    height: calc(100vh - 40px - 15px - 10px - 40px - 10px);
    overflow-y: auto;

    .item-box {
        width: 100%;
        padding: 10px;
        border-radius: 5px;
        background: #fbfbfb;
        margin-bottom: 6px;
        position: relative;
        cursor: pointer;

        .ps {
            color: #999999;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        &:hover {
            background: #fdf6f6;
        }
    }

    .acitve {
        background: #3e8af9;
        color: #fff;

        .ps {
            color: #DDDDDD;
        }

        &:hover {
            background: #3e8af9;
        }
    }

    .num_od {
        border-top: 1px solid #ede7e7;
        height: 40px;
        line-height: 40px;
        margin-top: 6px;
    }
}
</style>